﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.MonHocService;
namespace eTMS.Controllers{
public class MonHocControllerBase : CommonController{
	public MonHocControllerBase()
		: base()
	{	
		this.dao = new MonHocServiceClient();
	}
	~MonHocControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private MonHocEntity oMonHoc;	
	public MonHocEntity MonHocObject{
		get{
			return oMonHoc;
		}
		set{
			oMonHoc=value;
		}
	}
	protected MonHocServiceClient dao;
	public DataTable Table{
		get{		
			return _Table;
		}
        set
        {
            _Table = value;
        }
	}
	public void LoadAllDSMonHoc(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oMonHoc==null)return false;
		bool result=true;
		try{
			oMonHoc = dao.SaveRet(oMonHoc);
		}
		catch{
			result=false;
		}
		if(result){
			_ListMonHoc.Add(oMonHoc);
		}
		return result;
	}
	public bool Update(){
		if(oMonHoc==null)return false;
		return dao.Update(oMonHoc);
	}
	public bool Delete(){
		if(oMonHoc==null)return false;
		bool retvalue = dao.Delete(oMonHoc);
		if(retvalue){			
			_ListMonHoc.Remove(oMonHoc);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 MonHoc_ID){
		bool retvalue = dao.DeleteByID(MonHoc_ID);		
		if(retvalue){
			MonHocEntity oDeleted=FindMonHoc(MonHoc_ID);
			if(oDeleted!=null){
				_ListMonHoc.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oMonHoc = new MonHocEntity();
	}
	//Check if exists MaMonHoc
	public bool CheckExistsMaMonHoc(object value){	
		return dao.SelectOneByMaMonHoc(value.ToString()).MaMonHoc!=null;
    }    
    //Copy data from data row in grid to object
    public MonHocEntity MapDataRowToObject(DataRow row){
		MonHocEntity o = new MonHocEntity();
		 o.MaMonHoc=Convert.ToString(row["MaMonHoc"]);
		 o.TenMonHoc=Convert.ToString(row["TenMonHoc"]);
		 if(!row.IsNull("SoHocPhan"))
			o.SoHocPhan=ConvertNumber(row["SoHocPhan"]);
		 if(!row.IsNull("SoHocPhanHocPhi"))
			o.SoHocPhanHocPhi=ConvertNumber(row["SoHocPhanHocPhi"]);
         if (!row.IsNull("SoTietHoc"))
             o.SoTietHoc = ConvertNumber(row["SoTietHoc"]);
		 o.MoTa=Convert.ToString(row["MoTa"]);
		 if(!row.IsNull("BoMon_ID"))
			o.BoMon_ID=(Int32)(row["BoMon_ID"]);
		 if(!row.IsNull("MonHoc_ID"))
		    o.MonHoc_ID=(Int32)(row["MonHoc_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(MonHocEntity o,DataRow row){
		row["MaMonHoc"]=o.MaMonHoc;
		row["TenMonHoc"]=o.TenMonHoc;
		row["SoHocPhan"]=o.SoHocPhan;
		row["SoHocPhanHocPhi"]=o.SoHocPhanHocPhi;
        row["SoTietHoc"] = o.SoTietHoc;
		row["MoTa"]=o.MoTa;
		row["BoMon_ID"]=o.BoMon_ID;
		row["MonHoc_ID"]=o.MonHoc_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.MonHocObject,row);
    }
	public void LoadObject(Int32 MonHoc_ID){
		MonHocEntity oLoad=FindMonHoc(MonHoc_ID);
		if(oLoad==null){
			oMonHoc=this.LoadMonHoc(MonHoc_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oMonHoc.IsNullMonHoc_ID;
			if(!bNotIsNull){
				this.ListMonHoc.Add(oMonHoc);
			}
		}
		else{
			oMonHoc=oLoad;
		}
	}
	#region CachedList MonHoc
	private static List<MonHocEntity> _ListMonHoc=new List<MonHocEntity>();
    public List<MonHocEntity>ListMonHoc{
		get{
			return _ListMonHoc;
		}
	}
	public static List<MonHocEntity>ListMonHocStatic{
		get{
			return _ListMonHoc;
		}
	}
	public MonHocEntity FindMonHoc(Int32 MonHoc_ID){
		MonHocEntity oResult=this.ListMonHoc.Find(delegate(MonHocEntity oFind){
			bool result=true; 
			result&=oFind.MonHoc_ID.Equals(MonHoc_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllMonHoc(bool clearCache){
		if(_ListMonHoc.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		MonHocServiceClient dao = new MonHocServiceClient();
		_ListMonHoc=new List<MonHocEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public MonHocEntity LoadMonHoc(Int32 MonHoc_ID){			
		MonHocEntity o=dao.SelectOne(MonHoc_ID);		
		return o;
	}
	#endregion
}
}
